#!/bin/bash

APPDIR="${HOME}"/.local/share/applications

#Finding your default wineprefix directory
if [ -z "${WINEPREFIX}" ]; then
    PREFIXDIR="${HOME}"/.wine #if "WINEPREFIX=" envvar is unset or set to empty string, wineprefix location is "~/.wine"
else
    PREFIXDIR="${WINEPREFIX}" #if "WINEPREFIX=" envvar was previously set, wineprefix location is "$WINEPREFIX"
fi

#Unregistering OCX/DLL files from your wineprefix if they exist
OCXDIR="${PREFIXDIR}"/drive_c/VARA/OCX
for ACTIVEXFILE in "${OCXDIR}/COMDLG32.OCX" "${OCXDIR}/MSCHRT20.OCX" "${OCXDIR}/MSCOMCTL.OCX" "${OCXDIR}/MSCOMM32.OCX" "${OCXDIR}/MSSTDFMT.DLL" "${OCXDIR}/MSWINSCK.OCX"; do
    if [ -f "${ACTIVEXFILE}" ]; then
        BOX86_NOBANNER=1 BOX64_NOBANNER=1 WINEDEBUG=-all wine regsvr32 "${PREFIXDIR}"/drive_c/VARA/OCX/*.*[A-Z] /u /s || warning 'Could not unregister VARA HF VB6 libraries with your wineprefix' # the lower-case OCX/DLL files don't have built-in register/unregister functions
        break
    fi
done

#Removing program files (keeping any VARA.ini settings files)
mv -f "${PREFIXDIR}"/drive_c/VARA/VARA.ini /tmp/VARA.ini 2>/dev/null
rm -rf "${PREFIXDIR}"/drive_c/VARA/* 2>/dev/null || warning 'Could not remove VARA program files. If you moved VARA within Wine or made a custom wineprefix, you might have to remove VARA manually.'
mv /tmp/VARA.ini "${PREFIXDIR}"/drive_c/VARA/VARA.ini 2>/dev/null
rmdir "${PREFIXDIR}/drive_c/VARA" 2>/dev/null || true #delete directory if empty, prevent error code

#Removing Desktop Entry
rm "${APPDIR}"/VARA/varahf.desktop 2>/dev/null || warning 'Could not remove menu button. You might need to do this manually if you moved it.'
rmdir "${APPDIR}/VARA" 2>/dev/null || true #delete directory if empty, prevent error code
